clear; close all;


% event window
NOBS_before = 24;
NOBS_after  = 10*12;


% load crisis simulation
load crisis_simulation_out.mat MOM6 MOM8 MOM9 MOM10 MOM11 state1 state2


% Figure 4
ff = figure('Position', [100, 100, 800, 730]);
set(ff,'Units','Inches');
pos = get(ff,'Position');
set(ff,'PaperPositionMode','Auto','PaperUnits','Inches','PaperSize',[pos(3), pos(4)])
subplot = @(m,n,p)subtightplot(m, n, p, [0.13 0.13], [0.07 0.04], [0.025 0.01]);

% load data
MOM6(NOBS_before+1,:) = MOM6(NOBS_before+2,:);
load('model_solution.mat','omega');

subplot(2,4,[1,2]);
plot((-NOBS_before:NOBS_after-1)/12,mean(MOM8,2)/5,'r',(-NOBS_before:NOBS_after-1)/12,mean(MOM9+MOM8,2)/5,'k:','LineWidth',1.5);
set(gca,'FontSize',12); title('5-Year Default Probability, % p.a.','FontSize',15); xlim([-1,7]); grid on;
xlabel('Event Time (Years)','FontSize',15); ylim([0,4.5]);
legend('P','Q','Location','NE','Orientation','horizontal');

subplot(2,4,[3,4]);
plot((-NOBS_before:NOBS_after-1)/12,mean(MOM10,2),'r',(-NOBS_before:NOBS_after-1)/12,mean(MOM11,2),'k:','LineWidth',1.5);
set(gca,'FontSize',12); title('5-year Loss Given Default, % p.a.','FontSize',15); xlim([-1,7]); grid on;
xlabel('Event Time (Years)','FontSize',15); ylim([60,100]);
legend('P','Q','Location','NE','Orientation','horizontal');

subplot(2,4,[6,7]);
plot((-NOBS_before:NOBS_after-1)/12,nanmean(MOM6,2),'r',(-NOBS_before:NOBS_after-1)/12,100*(mean(state1,2)*omega(1)+mean(state2,2)*omega(2)+mean(1-state1-state2,2)*omega(3)),'k:','LineWidth',1.5);
set(gca,'FontSize',12); title('Realized Losses, %','FontSize',15); xlim([-1,7]); grid on; xlabel('Event Time (Years)','FontSize',15); ylim([0,100]);
legend('Loss rate','Bankruptcy costs','Location','SE','Orientation','horizontal');

print(gcf,'figure_4','-dpdf','-r0')


